<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.beans.AdministratorBean" %>
<%@page import="com.spartancoder.accommodation.models.Administrator" %>
<%
    String error = null;

    String name = request.getParameter("name");
    String pass = request.getParameter("password");
    String passConf = request.getParameter("passwordConfirm");

    if( name != null && pass != null && passConf != null ) {
        if( name.trim().equals("") || pass.trim().equals("") ) {
            error = "Toate campurile trebuie sa fie completate";
        } else {
            if( pass.equals(passConf) == false ) {
                error = "Parola confirmata nu corespunde";
            } else {
                if( Administrator.exists(name) ) {
                    error = "Exista deja un cont cu acest nume";
                } else {
                    AdministratorBean ab = new AdministratorBean();
                    ab.setUser(name);
                    Administrator.create(ab, pass);
                    session.setAttribute("flash", "Contul a fost create cu succes");
                    response.sendRedirect("list-admin.jsp");
                }
            }
        }
    } else {
        name = "";
    }
%>

<h2>Adaugare cont administrativ</h2>

<form action="add-admin.jsp" method="post" class="span-10">
    <fieldset>
        <% if (error !=null) { %>
        <br /><div class="error"><%=error%></div>
        <% } %>

        <label class="text large">Nume:</label><br />
        <input type="text" name="name" class="text large" value="<%=name%>" /><br />

        <label class="text large">Parola:</label><br />
        <input type="password" name="password" class="text large" /><br />

        <label class="text large">Confirmare parola:</label><br />
        <input type="password" name="passwordConfirm" class="text large" /><br />

        <button type="submit" class="button positive">
            Adauga cont
        </button>
    </fieldset>
</form>

<%@include file="_footer.jspf" %>